#!/usr/bin/python3
# ******************************************************************************
# Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
# licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# ******************************************************************************/
from python_learn.obs_api import obs_get, obs_post


class PackageRelate:
    def __init__(self):
        self.target_project = "openEuler:Mainline"
        self.repository = "standard_aarch64"
        self.arch = "aarch64"
        self.package = "python-mock"

    def get_package_src_and_bin(self, target_project=None, package=None):
        """
        获取软件包编译生成的源码包和二进制包
        :return:
        """
        target_project = self.target_project if target_project is None else target_project
        package = self.package if package is None else package
        url = f"/build/{target_project}/{self.repository}/{self.arch}/{package}"
        result = obs_get(url=url)
        # print(result)
        return result

    def get_package_provides_and_requiredby(self, rpm, target_project=None, package=None, ):
        """
        获取二进制包提供的组件以及他们的被依赖软件包
        :return:
        """
        target_project = self.target_project if target_project is None else target_project
        package = self.package if package is None else package
        url = f"/build/{target_project}/{self.repository}/{self.arch}/{package}/{rpm}?view=fileinfo_ext"
        result = obs_get(url=url)
        # print(result)
        return result

    def get_package_requires(self):
        """
        获取软件包的依赖信息
        :return:
        """
        data = {"view": "order"}
        result = obs_post(url=f'build/{self.target_project}/{self.repository}/{self.arch}/_builddepinfo', data=data)
        print(result)

    def package_test(self):
        url = f"/build/{self.target_project}/{self.repository}/{self.arch}/{self.package}/_buildinfo"
        result = obs_get(url=url)
        print(result)


if __name__ == '__main__':
    package_instance = PackageRelate()
    # package_instance.get_package_requires()
    # package_instance.get_package_requires()
    # result = package_instance.get_package_src_and_bin()
    result = package_instance.get_package_provides_and_requiredby(rpm='python3-mock-4.0.3-1.oe1.noarch.rpm')
    # package_instance.package_test()
    print(result)
